package com.rudolfheszele.smsselector.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SmsSelectorRuleSetSQLiteHelper extends SQLiteOpenHelper
{
	public static final String DATABASE_NAME = "smsselector";
	public static final int DATABASE_VERSION = 5;
	
	public class Tables
	{
		public static final String RULE_SETS = "RuleSets";
		public static final String RULES = "Rules";
		public static final String OPERATIONS = "Operations";
	}
	
	public class Columns
	{
		public static final String ID = "id";
		public static final String RULE_SET = "rule_set";
		public static final String NAME = "name";
		public static final String ADDITONAL_INFO = "additional_info";
		public static final String ACTION = "action";
	}
	
	public SmsSelectorRuleSetSQLiteHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database)
	{
		String command = "CREATE TABLE IF NOT EXISTS ";
		
		database.execSQL(command + Tables.RULE_SETS + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), action INTEGER, additional_info VARCHAR(50))");
		database.execSQL(command + Tables.RULES + " (rule_set INTEGER, id INTEGER, additional_info VARCHAR(50))");
		database.execSQL(command + Tables.OPERATIONS + " (rule_set INTEGER, id INTEGER)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		String command = "DROP TABLE IF EXISTS ";
		
		db.execSQL(command + Tables.RULE_SETS);
		db.execSQL(command + Tables.RULES);
		db.execSQL(command + Tables.OPERATIONS);
        onCreate(db);
	}

}
